package com.dashu.authority.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dashu.authority.common.Constant;
import com.dashu.authority.entity.Menu;
import com.dashu.authority.exception.AuthorityException;
import com.dashu.authority.servie.MenuService;
import com.dashu.authority.util.ResultBean;
import com.dashu.authority.util.ResultMsgUtil;
import com.dashu.authority.util.ReturnMessage;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController{
	
	
	@Resource
	private MenuService menuService;
	
	@RequestMapping("/findAllByPaging")
	public @ResponseBody Object findAllByPaging(Menu entity,Integer page,Integer rows)throws AuthorityException{
		List<Menu> returnList=menuService.queryAllByExample(entity, page, rows);
		Map<String, Object> returnMap = ResultMsgUtil.datagridFormat(new PageInfo<Menu>(returnList));
		return new ResultBean(Constant.STATUS_NORMAL,returnMap).getResultMap();
	}
	
	@RequestMapping("/queryIdNotSamePId")
	public @ResponseBody Object queryIdNotSamePId(Menu entity,Integer page,Integer rows)throws AuthorityException{
		List<Menu> returnList=menuService.queryIdNotSamePId(entity, page, rows);
		Map<String, Object> returnMap = ResultMsgUtil.datagridFormat(new PageInfo<Menu>(returnList));
		return new ResultBean(Constant.STATUS_NORMAL,returnMap).getResultMap();
	}
	
	@RequestMapping(value = "/save")
	public @ResponseBody Object addMenu(Menu entity) throws AuthorityException {  	
    	Map<String, Object>  returnMap =  menuService.insertEntity( entity );
		return new ResultBean(returnMap).getResultMap();
    }

    @RequestMapping( value = "/update" )
    public @ResponseBody Object updateMenu(Menu entity)  throws AuthorityException {  	
    	Map<String, Object> returnMap = menuService.updateEntity(entity);
		return new ResultBean(returnMap).getResultMap();
    }


    @RequestMapping( value = "/deleteByIds" )
    public @ResponseBody Object deleteByIds(@RequestParam(value = "ids") Integer[] ids)  throws AuthorityException {    	
		Map<String, Object> returnMap = menuService.deleteByIds(ids);
		return new ResultBean(returnMap).getResultMap();
    }
    
    @RequestMapping( value = "/updateStatus" )
    public @ResponseBody Object updateStatus(@RequestParam(value = "ids") Integer[] ids,String status)  throws AuthorityException { 
    	Map<String, Object> returnMap = menuService.updateStatus(ids,status);
		return new ResultBean(returnMap).getResultMap();
    }
    
    
    /**
     * 
     * @param
     * @return
     * @throws AuthorityException 
     */
    @RequestMapping("/queryMenuTreeList")
    public  @ResponseBody Object  queryMenuTreeList(Integer applicationId) throws AuthorityException {
    	Map<Object, Object> paramsMap = new HashMap<Object, Object>();
    	paramsMap.put("applicationId",applicationId);
    	List<Menu> list = menuService.queryMenuTreeList(paramsMap);
    	String returnJson = ReturnMessage.getJsonString(list);	
		return returnJson;
    }
    
}
